'''
Created on Apr 22, 2012

@author: samindaw

'''

from net.pycbr.CBR import KScriptBase, KBScriptEngine, read_script
import os

def show_result(engine, sentences):
    print "Original sentences"
    print "------------------"
    for sentence in sentences:print sentence
    print "Matches"
    print "-------"
    results = engine.search_script(sentences,n=10)
    results = engine.merge_script(results[0][0], sentences)
    for result in results[0]:print result
    print 

sb=KScriptBase()

#load scripts
script_path="resources/samples/scripts/"
if (not os.path.isdir("resources")):
    script_path="../"+script_path
sb.add_script(read_script(script_path+"Accident"))
sb.add_script(read_script(script_path+"beach.txt"))
sb.add_script(read_script(script_path+"concert.txt"))
sb.add_script(read_script(script_path+"Eating_Lunch"))
sb.add_script(read_script(script_path+"going_to_work"))
sb.add_script(read_script(script_path+"grumpy_morning.txt"))
sb.add_script(read_script(script_path+"gym"))
sb.add_script(read_script(script_path+"hard_boil_eggs"))
sb.add_script(read_script(script_path+"house_fire"))
sb.add_script(read_script(script_path+"In_office.txt"))
sb.add_script(read_script(script_path+"library.txt"))
sb.add_script(read_script(script_path+"making_coffee.txt"))
sb.add_script(read_script(script_path+"park.txt"))
sb.add_script(read_script(script_path+"Playing_soccer"))
sb.add_script(read_script(script_path+"script_essay_writing.txt"))
sb.add_script(read_script(script_path+"script_tea_making.txt"))
sb.add_script(read_script(script_path+"sick.txt"))
sb.add_script(read_script(script_path+"studying_newspaper"))
sb.add_script(read_script(script_path+"taking_pictures.txt"))
sb.add_script(read_script(script_path+"watching_tv"))

#create script engine
engine=KBScriptEngine(sb)

#run tests

print "Jailbird"
print "========"
sentences=[]
sentences.append({"subject":"prisoner","action":"take","utility":"key,guard"})
sentences.append({"subject":"prisoner","action":"open","utility":"jail,cell"})
sentences.append({"subject":"guard","action":"sleep","utility":"chair"})
show_result(engine, sentences)

print "A Happy Ending?"
print "==============="
sentences2=[]
sentences2.append({"subject":"boy","action":"see","utility":"girl"})
sentences2.append({"subject":"boy","action":"say","utility":"love"})
sentences2.append({"subject":"father","action":"come","utility":"girl"})
sentences2.append({"subject":"father","action":"beat","utility":"boy"})
show_result(engine, sentences2)


print "Robbery"
print "======="
sentences3=[]
sentences3.append({"subject":"woman","action":"go","utility":"shortcut"})
sentences3.append({"subject":"man","action":"take","utility":"money"})
sentences3.append({"subject":"man","action":"shoots","utility":"woman"})
sentences3.append({"subject":"man","action":"run","utility":"police"})
show_result(engine, sentences3)

print "Heart Attack"
print "============"
sentences4=[]
sentences4.append({"subject":"man","action":"run","utility":"beach"})
sentences4.append({"subject":"man","action":"feel","utility":"pain"})
sentences4.append({"subject":"man","action":"fall","utility":"ground"})
sentences4.append({"subject":"ambulance","action":"take","utility":"hospital"})
sentences4.append({"subject":"man","action":"survives","utility":"attack"})
show_result(engine, sentences4)